package tz.preprocessing;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import tz.data.Article;
import tz.preprocessing.lexical.WordExtract;
import tz.preprocessing.stopwords.StopWordsExtract;
import tz.preprocessing.suffixStripping.Stemmer;

/**
 * 
 * @author dejvid
 * 
 * 		1. Preberemo vse testne članke.
 * 		2. Iz člankov odstranimo stopwords
 * 		3. poženemo stemmer nad preostalimi besedami
 * 		4. naredimo seznam besed za vsak članek
 *
 */
public class Article2Words
{
	
	public static String stopWordsFile;
	public static void main(String[] args)
	{
		StopWordsExtract stopWords = new StopWordsExtract(new File(stopWordsFile));
		Map<Article, List<String>> articleMap = new HashMap<Article, List<String>>();
		
		//1. Preberemo vse članke
		List<Article> lstArticles = WordExtract.getAllArticles();
		//Vse članke pretvorimo v besede. Besede iz člankov odstranimo, ki so stop words!
		for(Article article : lstArticles)
		{
			LinkedList<String> lstWords = new LinkedList<String>();
			article.getWordsFromArticle(lstWords);
			removeStopWords(lstWords, stopWords);
			lstWords = Stemmer.stemAListofWords(lstWords);
			articleMap.put(article, lstWords);
		}
		printMap(articleMap);
	}
	
	public static void printMap(Map<Article, List<String>> articleMap)
	{
		Set<Article> keys = articleMap.keySet();
		for(Article article : keys)
		{
			System.out.print(article.getArticleID() + ": ");
			List<String> artWords = articleMap.get(article);
			printWords(artWords);
			System.out.println();
		}
	}
	
	public static void printWords(List<String> list)
	{
		for(String s : list)
		{
			System.out.print(s + " ");
		}
	}
	
	
	/**
	 * Iz words odstrani tiste besede, ki so stopwords.
	 * @param words 
	 * @param stop
	 */
	public static void removeStopWords(List<String> words, StopWordsExtract stop)
	{
		int i = 0;
		while(i < words.size())
		{
			String w = words.get(i);
			if(stop.isStopWord(w))
			{
				words.remove(i);
			}
			else //not a stopword
			{
				i++;
			}
		}
	}
}
